package com.danger.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.danger.Adapter.StuAdapter;
import com.danger.Model.Student;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button add_button;
    private ListView listView;
    private StuAdapter adapter;
    private List<Student> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();

    }

    private void initView() {
        add_button = (Button) findViewById(R.id.add_button);
        listView = (ListView) findViewById(R.id.listView);
        list = new ArrayList<>();
        adapter = new StuAdapter(this,list);
        listView.setAdapter(adapter);
        for (int i = 0; i < 8; i++) {
            Student student = new Student();
            student.setName("danger" + i);
            student.setAge(10 + i);
            student.save();
            list.add(student);
        }
        adapter.notifyDataSetChanged();
    }

    private void initEvent() {
        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student student = new Student();
                student.setName("feng");
                student.setAge(10);
                student.save();
                list.add(student);
                adapter.notifyDataSetChanged();
            }
        });
    }
}